package mafia.younoodle.ilovemystuff;

import java.util.ArrayList;

import mafia.younoodle.ilovemystuff.data.Category;
import mafia.younoodle.ilovemystuff.data.DataFetcher;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

public final class CategoryListActivity extends ListActivity {

    private TextView empty;
    private ProgressDialog progressDialog;
    private CategoryListAdapter categoryListAdapter;
    private ArrayList<Category> categories;
    
    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(final Message msg) {
            Log.v("STUFF", "Worker thread done");
            progressDialog.dismiss();
            if ((categories == null) || (categories.size() == 0)) {
                empty.setText("No Data");
            } else {
//                categoryListAdapter = new CategoryListAdapter((Context) CategoryListActivity.this, R.layout.category_list, categories);
                categoryListAdapter = new CategoryListAdapter((Context) CategoryListActivity.this, categories);
                setListAdapter(categoryListAdapter);
            }
        }
    };
    
    @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
        this.setContentView(R.layout.category_list);
        this.empty = (TextView) findViewById(R.id.empty);
		
        final ListView listView = getListView();
        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setEmptyView(this.empty);
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.v("STUFF", "conResume()");
		
		// TODO get current location from Application
		
		//int startFrom = getIntent().getIntExtra("mafia.younoodle.ilovemystuff.StartFrom", 1);
		//loadCategories();
		
		final DataFetcher fetcher = new DataFetcher();
		ArrayList<Category> categories = fetcher.getCategories();
		
		
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
//        menu.add(0, ReviewList.MENU_GET_NEXT_PAGE, 0, R.string.menu_get_next_page).setIcon(
//            android.R.drawable.ic_menu_more);
//        menu.add(0, ReviewList.MENU_CHANGE_CRITERIA, 0, R.string.menu_change_criteria).setIcon(
//            android.R.drawable.ic_menu_edit);
        return true;
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
//        Intent intent = null;
//        switch (item.getItemId()) {
//            case MENU_GET_NEXT_PAGE:
//                // increment the startFrom value and call this Activity again
//                intent = new Intent(Constants.INTENT_ACTION_VIEW_LIST);
//                intent.putExtra(Constants.STARTFROM_EXTRA, getIntent().getIntExtra(Constants.STARTFROM_EXTRA, 1)
//                    + ReviewList.NUM_RESULTS_PER_PAGE);
//                startActivity(intent);
//                return true;
//            case MENU_CHANGE_CRITERIA:
//                intent = new Intent(this, ReviewCriteria.class);
//                startActivity(intent);
//                return true;
//        }
        return super.onMenuItemSelected(featureId, item);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
//        // set the current review to the Application (global state placed there)
//        RestaurantFinderApplication application = (RestaurantFinderApplication) getApplication();
//        application.setCurrentReview(this.reviews.get(position));
//
//        // startFrom page is not stored in application, for example purposes it's a simple "extra"
//        Intent intent = new Intent(Constants.INTENT_ACTION_VIEW_DETAIL);
//        intent.putExtra(Constants.STARTFROM_EXTRA, getIntent().getIntExtra(Constants.STARTFROM_EXTRA, 1));
//        startActivity(intent);
    }

    private void loadCategories() {
    	Log.v("STUFF", "loadCategories()");

        final DataFetcher fetcher = new DataFetcher();
        this.progressDialog = ProgressDialog.show(this, " Working...", " Retrieving categories", true, false);

        // get categories in a separate thread for ProgressDialog/Handler
        // when complete send "empty" message to handler
        new Thread() {
            @Override
            public void run() {
                categories = fetcher.getCategories();
                handler.sendEmptyMessage(0);
            }
        }.start();
    }    
	
}
